Golang encoding/xml.Encoder.Flush() function example
package encoding/xml
Flush flushes any buffered XML to the underlying writer. See the EncodeToken documentation for details about when it is necessary.
Golang encoding/xml.Encoder.Flush() function usage example
package main
import (
"encoding/xml"
"fmt"
"os"
"reflect"
)
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
encoder := xml.NewEncoder(os.Stdout)
newtoken := xml.StartElement{
Name : xml.Name{
Space: "",
Local: "location",
},
}
if err := encoder.EncodeToken(newtoken); err != nil {
fmt.Println(err)
}
start := xml.StartElement{
Name: xml.Name{
Space: "",
Local: reflect.Indirect(reflect.ValueOf(v)).Type().Name(),
},
Attr: []xml.Attr{{xml.Name{"", "xmlns"}, "https://socketloop.com/xmldoc/2014-09-01/"}},
}
if err := encoder.EncodeElement(v, start); err != nil {
fmt.Println(err)
}
encoder.Flush() // <-- here
}
Reference :
Advertisement
Something interesting
Tutorials
+16.5k Golang : Get IP addresses of a domain name
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+12.8k Golang : http.Get example
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+5.3k Javascript : Change page title to get viewer attention
+10.2k Golang : Check a web page existence with HEAD request example
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command
+24.5k Golang : GORM read from database example
+10.4k Golang : Generate random integer or float number
+6.1k Golang : How to write backslash in string?
+22k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+36.4k Golang : Convert date or time stamp from string to time.Time type